<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      //这是排列问题，[1,2,1] 和[1,1,2]是不同的排列。排列问题外层背包容量，内层物品。因此必须多个if dp[0]=1
      var combinationSum4 = function (nums, target) {
        let bagSize = target;
        let dp = new Array(bagSize + 1).fill(0);
        dp[0] = 1;
        for (let j = 0; j <= bagSize; j++) {
          for (let i = 0; i < nums.length; i++) {
            if (j >= nums[i]) {
              dp[j] += dp[j - nums[i]];
            }
          }
        }
        return dp[bagSize];
      };
    </script>
  </body>
</html>
